package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.Objects;

import cn.hutool.core.lang.Dict;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.constant.NoticeConstants;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.WebsocketUtils;
import com.ruoyi.common.websocket.MessageEndpoint;
import com.ruoyi.common.websocket.SocketMessageVo;
import com.ruoyi.system.domain.SysNoticeStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.SysNotice;
import com.ruoyi.system.mapper.SysNoticeMapper;
import com.ruoyi.system.service.ISysNoticeService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 公告 服务层实现
 *
 * @author ruoyi
 */
@Service
public class SysNoticeServiceImpl implements ISysNoticeService {
    @Autowired
    private SysNoticeMapper noticeMapper;

    /**
     * 查询公告信息
     *
     * @param noticeId 公告ID
     * @return 公告信息
     */
    @Override
    public SysNotice selectNoticeById(Long noticeId) {
        return noticeMapper.selectNoticeById(noticeId);
    }

    /**
     * 查询公告列表
     *
     * @param notice 公告信息
     * @return 公告集合
     */
    @Override
    public List<SysNotice> selectNoticeList(SysNotice notice) {
        return noticeMapper.selectNoticeList(notice);
    }

    @Override
    public Dict selectDrawerList() {
        //查询未读、未删除的消息 用户消息与系统消息
        SysNotice params = new SysNotice();
        params.setStatus("0");
        params.setReaded(false);
        params.setRemoved(false);
        params.setToUserName(SecurityUtils.getUsername());

        //用户消息
        params.setNoticeType(NoticeConstants.TYPE_USER);
        Page userPage = PageHelper.startPage(1, 5);
        noticeMapper.selectNoticeList(params);
        //系统消息
        params.setNoticeType(NoticeConstants.TYPE_SYSTEM);
        Page systemPage = PageHelper.startPage(1, 5);
        noticeMapper.selectNoticeList(params);
        //通知公告消息
        params.setNoticeType(NoticeConstants.TYPE_NOTICE);
        params.setToUserName(null);
        Page noticePage = PageHelper.startPage(1, 5);
        noticeMapper.selectNoticeList(params);

        //返回结果
        return Dict.create()
                .set("userTotal", userPage.getTotal())
                .set("userList", userPage.getResult())
                .set("systemTotal", systemPage.getTotal())
                .set("systemList", systemPage.getResult())
                .set("noticeTotal", noticePage.getTotal())
                .set("noticeList", noticePage.getResult());
    }

    /**
     * 新增公告
     *
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public int insertNotice(SysNotice notice) {
        int i = noticeMapper.insertNotice(notice);
        //发送给所有用户，有新的通知
        if (Objects.equals(notice.getNoticeType(), NoticeConstants.TYPE_NOTICE)) {
            WebsocketUtils.notifyToReloadMessageDrawer();
        }
        return i;
    }

    /**
     * 修改公告
     *
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public int updateNotice(SysNotice notice) {
        int i = noticeMapper.updateNotice(notice);
        //发送给所有用户，有新的通知
        if (Objects.equals(notice.getNoticeType(), NoticeConstants.TYPE_NOTICE)) {
            WebsocketUtils.notifyToReloadMessageDrawer();
        }
        return i;
    }

    /**
     * 删除公告对象
     *
     * @param noticeId 公告ID
     * @return 结果
     */
    @Override
    public int deleteNoticeById(Long noticeId) {
        return noticeMapper.deleteNoticeById(noticeId);
    }

    /**
     * 批量删除公告信息
     *
     * @param noticeIds 需要删除的公告ID
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteNoticeByIds(Long[] noticeIds) {
        //先删除关联内容
        noticeMapper.deleteNoticeStatusByNoticeIds(noticeIds);
        //然后删除消息本体
        return noticeMapper.deleteNoticeByIds(noticeIds);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void setReaded(List<Long> noticeIds) {
        String userName = SecurityUtils.getUsername();
        for (Long noticeId : noticeIds) {
            SysNoticeStatus noticeStatus = noticeMapper.getSelfStatus(userName, noticeId);
            if (noticeStatus != null) {
                //更新状态为已读
                noticeStatus.setReaded(true);
                noticeMapper.updateNoticeStatus(noticeStatus);
            } else {
                //新增状态，设为已读
                noticeStatus = new SysNoticeStatus();
                noticeStatus.setReaded(true);
                noticeStatus.setUserName(userName);
                noticeStatus.setNoticeId(noticeId);
                noticeMapper.insertNoticeStatus(noticeStatus);
            }
        }
    }

    @Override
    public void setRemoved(List<Long> noticeIds) {
        String userName = SecurityUtils.getUsername();
        for (Long noticeId : noticeIds) {
            SysNoticeStatus noticeStatus = noticeMapper.getSelfStatus(userName, noticeId);
            if (noticeStatus != null) {
                //更新状态为已读
                noticeStatus.setRemoved(true);
                noticeMapper.updateNoticeStatus(noticeStatus);
            } else {
                //新增状态，设为已读
                noticeStatus = new SysNoticeStatus();
                noticeStatus.setRemoved(true);
                noticeStatus.setUserName(userName);
                noticeStatus.setNoticeId(noticeId);
                noticeMapper.insertNoticeStatus(noticeStatus);
            }
        }
    }
}
